package com.glory.brepda.adapter;
import android.widget.LinearLayout;

import androidx.annotation.Nullable;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.glory.brepda.R;
import com.glory.brepda.base.MyApp;
import com.glory.brepda.entity.RecipeInfoEntity;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_LogUtils;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_ToastUtils;

import java.util.List;

/**
 * FileName: PERecipeListAdapter
 * Author: XJH
 * Date: 2020/6/18 17:01
 * Description:
 * History:
 */
public class PERecipeListAdapter extends BaseQuickAdapter<RecipeInfoEntity, BaseViewHolder> {

    private int mSelectorPos = -1;

    public PERecipeListAdapter(@Nullable List<RecipeInfoEntity> data) {
        super(R.layout.item_bin_info_list,data);
    }

    @Override
    protected void convert(BaseViewHolder helper, RecipeInfoEntity item) {
        helper.setText(R.id.list_tv1,item.getRecipeEqpID());
        helper.setText(R.id.list_tv2,item.getRecipeID());
        helper.setText(R.id.list_tv3,item.getRecipeVer());
        helper.setText(R.id.list_tv4,item.getStatus().equals("0") ? "失效" : "有效");
//        helper.setText(R.id.list_tv5,item.getChipName());
//        helper.setText(R.id.list_tv6,item.getWireType());
        helper.setGone(R.id.list_tv4,true);
//        helper.setGone(R.id.list_tv5,true);
//        helper.setGone(R.id.list_tv6,true);
        helper.setBackgroundColor(R.id.llItemLayout,item.isSelector() ?
                MyApp.getInstance().getResources().getColor(R.color.free_ui_glory_theme_color) :
                MyApp.getInstance().getResources().getColor(R.color.white));

        LinearLayout mItemLayout = helper.getView(R.id.llItemLayout);
        mItemLayout.setOnClickListener(view -> {
            FreeApi_LogUtils.saveErrorMessages(mSelectorPos+"","setOnClickListener");

            if (mSelectorPos == -1){
                mSelectorPos = helper.getAdapterPosition();
                item.setSelector(true);
            }else if (mSelectorPos == helper.getAdapterPosition()){
//                FreeApi_ToastUtils.showFreeToast("点击相同条目",mContext,true,0);
            }else if (mSelectorPos != helper.getAdapterPosition()){
                getData().get(mSelectorPos).setSelector(false);
                item.setSelector(true);
                mSelectorPos = helper.getAdapterPosition();
            }

            notifyDataSetChanged();
        });
    }

    public RecipeInfoEntity getSelectorData(){
        if (mSelectorPos == -1){
            return null;
        }
        FreeApi_LogUtils.saveErrorMessages(mSelectorPos+"","getSelectorData");

        return getData().get(mSelectorPos);
    }
}
